/**
 * 将文件转为blob
 * @param {*} file 文件对象
 */
export function getImgUrl(file) {
  return Promise.resolve().then(() => URL.createObjectURL(file));
}

/**
   * 将给定数据进行文件下载
   * @param {*} data 数据
   * @param {*} filename 文件名
   * @param {*} mime 类型
   */
export const download = function(data, filename, mime) {
  const blob = new Blob([data], { type: mime || "application/octet-stream" });
  if (typeof window.navigator.msSaveBlob !== "undefined") {
    window.navigator.msSaveBlob(blob, filename);
  } else {
    const blobURL = window.URL.createObjectURL(blob);
    const tempLink = document.createElement("a");
    tempLink.style.display = "none";
    tempLink.href = blobURL;
    tempLink.setAttribute("download", filename);

    if (typeof tempLink.download === "undefined") {
      tempLink.setAttribute("target", "_blank");
    }

    document.body.appendChild(tempLink);
    tempLink.click();
    document.body.removeChild(tempLink);
    window.URL.revokeObjectURL(blobURL);
  }
};